package com.ruoyi.drawing.controller;

import javax.annotation.Resource;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.drawing.domain.DtsDrawingModelRegion;
import com.ruoyi.drawing.service.IDtsDrawingModelRegionService;
import com.ruoyi.drawing.api.params.DrawingModelDocumentParams;
import com.ruoyi.drawing.api.query.DrawingModelDocumentQuery;
import com.ruoyi.drawing.api.vo.DrawingModelDocumentVO;
import com.ruoyi.drawing.api.vo.FileVO;
import com.ruoyi.drawing.api.vo.RegionDownloadsUploadsVO;
import com.ruoyi.drawing.api.vo.StorageModelQuantityVO;
import io.jsonwebtoken.lang.Assert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.drawing.service.IDtsDrawingModelDocumentService;
import com.ruoyi.common.core.web.controller.BaseController;

import java.util.List;

/**
 * 图档中心模型文档
 *
 * @author ZhangYu
 * @date 2024-04-30
 */
@Api(tags = "图档中心模型文档")
@RestController
@RequestMapping("/document")
public class DtsDrawingModelDocumentController extends BaseController {

    @Resource
    private IDtsDrawingModelDocumentService drawingModelDocumentService;
    @Resource
    private IDtsDrawingModelRegionService drawingModelRegionService;

    /**
     * 查询图档中心模型文档分页
     *
     * @param drawingModelDocumentQuery 图档中心模型文档查询参数
     * @return 图档中心模型文档分页
     */
    @ApiOperation("查询图档中心模型文档分页")
    @GetMapping("/page")
    public AjaxResult drawingModelDocumentPage(DrawingModelDocumentQuery drawingModelDocumentQuery) {
        Page<DrawingModelDocumentVO> drawingModelDocumentPage = drawingModelDocumentService.drawingModelDocumentPage(drawingModelDocumentQuery);
        return AjaxResult.success(drawingModelDocumentPage);
    }

    /**
     * 通过图档编码查询文件分页信息
     *
     * @param pageNum      当前页码
     * @param pageSize     每页数量
     * @param documentCode 图档编码
     * @return 图档编码查询文件分页信息
     */
    @ApiOperation("通过图档编码查询文件分页信息")
    @GetMapping("/file/page/by/code")
    public R<Page<FileVO>> filePageByCode(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam String documentCode) {
        Page<FileVO> filePage = drawingModelDocumentService.filePageByCode(pageNum, pageSize, documentCode);
        return R.ok(filePage);
    }

    /**
     * 新增图档中心模型文档
     *
     * @param drawingModelDocumentParams 图档中心模型文档添加or修改参数
     * @return 新增信息
     */
    @ApiOperation("新增图档中心模型文档")
    @PostMapping("/add")
    public AjaxResult addDrawingModelDocument(@RequestBody DrawingModelDocumentParams drawingModelDocumentParams) {
        Assert.notNull(drawingModelDocumentParams.getModelRegionId(), "模型区域id不能为空");
        drawingModelDocumentService.addDrawingModelDocument(drawingModelDocumentParams);
        return AjaxResult.success();
    }

    /**
     * 修改图档中心模型文档
     *
     * @param drawingModelDocumentParams 图档中心模型文档添加or修改参数
     * @return 修改信息
     */
    @ApiOperation("修改图档中心模型文档")
    @PutMapping("/update")
    public AjaxResult updateDrawingModelDocument(@RequestBody DrawingModelDocumentParams drawingModelDocumentParams) {
        Assert.notNull(drawingModelDocumentParams.getId(), "id不能为空");
        drawingModelDocumentService.updateDrawingModelDocument(drawingModelDocumentParams);
        return AjaxResult.success();
    }

    /**
     * 删除图档中心模型文档
     *
     * @param id 唯一主键
     * @return 删除信息
     */
    @ApiOperation("删除图档中心模型文档")
    @DeleteMapping("/remove")
    public AjaxResult removeDrawingModelDocument(@RequestParam Long id) {
        drawingModelDocumentService.removeDrawingModelDocument(id);
        return AjaxResult.success();
    }

    /**
     * 获取图档中心模型文档详细信息
     *
     * @param id 唯一主键
     * @return 图档中心模型文档返回参数
     */
    @ApiOperation("获取图档中心模型文档详细信息")
    @GetMapping("/getInfo")
    public AjaxResult getInfo(@RequestParam Long id) {
        DrawingModelDocumentVO drawingModelDocument = drawingModelDocumentService.getDrawingModelDocumentInfo(id);
        return AjaxResult.success(drawingModelDocument);
    }

    /**
     * 存储模型数量
     *
     * @return 存储模型数量
     */
    @ApiOperation("存储模型数量")
    @GetMapping("/storage/model/quantity")
    public AjaxResult storageModelQuantity() {
        StorageModelQuantityVO storageModelQuantity = drawingModelDocumentService.storageModelQuantity();
        return AjaxResult.success(storageModelQuantity);
    }

    /**
     * 下载量排行
     *
     * @param rowsNumber 查询条数
     * @return 下载量排行
     */
    @ApiOperation("下载量排行")
    @GetMapping("/download/rankings")
    public AjaxResult downloadRankings(@RequestParam Integer rowsNumber) {
        List<DrawingModelDocumentVO> drawingModelDocumentList = drawingModelDocumentService.downloadRankings(rowsNumber);
        return AjaxResult.success(drawingModelDocumentList);
    }

    /**
     * 添加下载量
     *
     * @param drawingModelDocumentParams 图档中心模型文档添加or修改参数
     * @return 修改信息
     */
    @ApiOperation("添加下载量")
    @PutMapping("/add/downloads/number")
    public AjaxResult addDownloadsNumber(@RequestBody DrawingModelDocumentParams drawingModelDocumentParams) {
        if (ObjectUtil.isEmpty(drawingModelDocumentParams.getId())) {
            return AjaxResult.error("id不能为空");
        }
        drawingModelDocumentService.addDownloadsNumber(drawingModelDocumentParams.getId());
        return AjaxResult.success();
    }

    /**
     * 区域下载与上传量
     *
     * @param regionName 图档中心模型文档添加or修改参数
     * @return 区域下载与上传量
     */
    @ApiOperation("区域下载与上传量")
    @GetMapping("/region/downloads/uploads")
    public AjaxResult regionDownloadsUploads(@RequestParam String regionName) {
        DtsDrawingModelRegion drawingModelRegion = drawingModelRegionService.getOne(new QueryWrapper<DtsDrawingModelRegion>().lambda().eq(DtsDrawingModelRegion::getRegionName, regionName));
        if (ObjectUtil.isEmpty(drawingModelRegion)) {
            return AjaxResult.error("区域不存在");
        }
        RegionDownloadsUploadsVO regionDownloadsUploads = drawingModelDocumentService.regionDownloadsUploads(regionName);
        return AjaxResult.success(regionDownloadsUploads);
    }

}
